package com.common.ui.recycleview;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import androidx.annotation.ColorRes;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import com.common.R;
import com.common.ui.brvah.BaseQuickAdapter;

public class PullLoadMoreRecyclerView extends LinearLayout {

    private Context context;
    private RecyclerView recyclerView;
    private SwipeRefreshLayout swipeRefreshLayout;
    private RecyclerView.LayoutManager layoutManager;
    private boolean isRefreshing = true;//是否正在刷新(默认打开时就开始加载)
    private boolean isLoadingMore = false;//是否正在加载更多
    public PullLoadMoreListener pullLoadMoreListener;

    public PullLoadMoreRecyclerView(Context context) {
        this(context, null);
    }

    public PullLoadMoreRecyclerView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PullLoadMoreRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        this.context = context;
    }

    public void init(RecyclerView recyclerView, SwipeRefreshLayout swipeRefreshLayout) {

        this.recyclerView = recyclerView;
        this.swipeRefreshLayout = swipeRefreshLayout;
        swipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary, R.color.colorPrimary, R.color.colorPrimary);
        recyclerView.setHasFixedSize(true);//提高性能
        //recyclerView.setItemAnimator(new DefaultItemAnimator());//设置效果
        //注释了局部item更新动画 并且再设置默认局部更新动画false可以结局局部更新闪烁问题  默认的动画就是闪烁效果
        //recyclerView.setItemAnimator(new SlideInLeftAnimator());
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                if (pullLoadMoreListener != null) {
                    pullLoadMoreListener.onRefresh();
                }
            }
        });
    }

    //1设置下拉刷新 圆圈颜色
    public void setColorSchemeResources(@ColorRes int... colorResIds){
        if(swipeRefreshLayout != null){
            swipeRefreshLayout.setColorSchemeResources(colorResIds);
        }
    }

    //2设置布局管理器
    public void setLayoutManager(RecyclerView.LayoutManager layoutManager){
        recyclerView.setLayoutManager(layoutManager);
    }
    public interface PullLoadMoreListener {
        void onRefresh();

        void onLoadMore();
    }

    //3设置适配器
    public void setAdapter(BaseQuickAdapter baseQuickAdapter){
        if(baseQuickAdapter!=null){
            recyclerView.setAdapter(baseQuickAdapter);
        }
    }

}
